﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JeanFish.Data.Common
{
    public abstract class DbConnection : System.Data.Common.DbConnection
    {
        private System.Data.Common.DbConnection connection;
        private const string connectionStringKey = "decoratedProvider=";
        private string providerName;
        public string ProviderName
        {
            get
            {
                return this.providerName;
            }
        }
        public System.Data.Common.DbConnection Connection
        {
            get
            {
                return this.connection;
            }
            set
            {
                this.connection = value;
            }
        }
        public override int ConnectionTimeout
        {
            get
            {
                return this.Connection.ConnectionTimeout;
            }
        }
        protected override System.Data.Common.DbTransaction BeginDbTransaction(System.Data.IsolationLevel isolationLevel)
        {
            return this.Connection.BeginTransaction(isolationLevel);
        }

        public override void ChangeDatabase(string databaseName)
        {
            this.Connection.ChangeDatabase(databaseName);
        }

        public override void Close()
        {
            this.Connection.Close();
        }

        public override string ConnectionString
        {
            get
            {
                return connectionStringKey + this.ProviderName + ";" + Connection.ConnectionString;
            }
            set
            {
                this.ParseConnectionString(value);
            }
        }

        private void ParseConnectionString(string connectionString)
        {
            int index = connectionString.IndexOf(';');
            string[] myConnectionString = connectionString.Substring(0, index).Split('=');
            if (string.Compare(connectionStringKey, myConnectionString[0], true) != 0)
                throw new ArgumentException("错误的连接字符串。");
            this.providerName = myConnectionString[1];
            string baseConnectionString = connectionString.Substring(index + 1);

            System.Data.Common.DbProviderFactory factory = System.Data.Common.DbProviderFactories.GetFactory(this.ProviderName);
            this.Connection = factory.CreateConnection();
            this.Connection.ConnectionString = baseConnectionString;
        }

        protected override System.Data.Common.DbCommand CreateDbCommand()
        {
            throw new NotSupportedException();
        }

        public override string DataSource
        {
            get { return this.Connection.DataSource; }
        }

        public override string Database
        {
            get { return this.Connection.Database; }
        }

        public override System.ComponentModel.ISite Site
        {
            get
            {
                return this.Connection.Site;
            }
            set
            {
                this.Connection.Site = value;
            }
        }

        public override void Open()
        {
            this.Connection.Open();
        }

        public override string ServerVersion
        {
            get { return this.Connection.ServerVersion; }
        }

        public override System.Data.ConnectionState State
        {
            get { return this.Connection.State; }
        }

        public override void EnlistTransaction(System.Transactions.Transaction transaction)
        {
            this.Connection.EnlistTransaction(transaction);
        }

        public override System.Data.DataTable GetSchema()
        {
            return this.Connection.GetSchema();
        }

        public override System.Data.DataTable GetSchema(string collectionName)
        {
            return this.Connection.GetSchema(collectionName);
        }

        public override System.Data.DataTable GetSchema(string collectionName, string[] restrictionValues)
        {
            return this.Connection.GetSchema(collectionName, restrictionValues);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.Connection.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
